package com.flowable.listener;

import org.flowable.cmmn.engine.impl.process.ProcessInstanceService;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.TaskService;
import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.delegate.JavaDelegate;
import org.flowable.engine.runtime.ProcessInstance;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

import static org.flowable.task.service.delegate.BaseTaskListener.EVENTNAME_CREATE;

/**
 * 长沙先飞信息科技有限公司<br>
 * Created with IntelliJ IDEA.<br>
 * Date:2024/11/19<br>
 * Time:11:29<br>
 * 功能描述：<br>
 *
 * @author huang hou chun
 * @since JDK 1.8 <br>
 * Copyright (c) 2023 www.csxfrj.com System Incorporated All Rights Reserved.
 */
@Component
public class SkipNodeListener implements JavaDelegate {

    @Lazy
    @Resource
    private RuntimeService runtimeService;

    @Override
    public void execute(DelegateExecution execution) {
        if (execution.getEventName().equals(EVENTNAME_CREATE)) {
            // 获取流程发起人
            ProcessInstance processInstance = runtimeService.createProcessInstanceQuery()
                    .processInstanceId(execution.getProcessInstanceId())
                    .singleResult();
            String startUserId = processInstance.getStartUserId();
            System.out.println("startUserId:" + startUserId);
            execution.setVariable("skipNode", true);
        }
    }
}
